/**
 *  Copyright (C) 2012 by Development Solutions, Software (DSS)
 *  Released under the terms of the GNU General Public license version 3 or later
 */
package org.dss.gil.service;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import javax.swing.ImageIcon;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.dss.gil.util.Globals;
import org.dss.gil.util.generic.PropertyFileReader;
import org.dss.gil.view.Ide;

/**
 *  @author  Fernando Gil <lobho.gil@gmail.com>
 *  @since   September 2012
 *  @version 1.1
 */
public class InitializationService {
    
    private static Logger logger = Logger.getLogger(InitializationService.class.getName());
    private static InitializationService INSTANCE = null;
    private ValidationService validations = null;
    
    private InitializationService(){
        validations = ValidationService.getInstance();
    }
    
    public static InitializationService getInstance() {
        if (INSTANCE == null){
            INSTANCE = new InitializationService();
        }
        return INSTANCE;
    }
    
    public void initLogger(){
        if(Globals.IS_PROD){
            PropertyConfigurator.configure( getClass().getResourceAsStream(Globals.LOG4J_PROPERTIES) );
        }else{
            PropertyConfigurator.configure( "src"+Globals.LOG4J_PROPERTIES );
        }
        logger.info("Log4j Initialized");
    }

    public void loadUserConfigurations(){
        Globals.config = PropertyFileReader.getPropertyFileReader(Globals.USER_CONFIG_PATH);
        if(Globals.config == null){
            loadDefaultConfigurations();
        }else{
            logger.info("User Configurations Initialized");
        }  
    }
    
    private void loadDefaultConfigurations(){
        if(Globals.IS_PROD){
            Globals.config = PropertyFileReader.getPropertyFileReader(
                    getClass().getResourceAsStream(Globals.DEF_CONFIG_PATH));
        }else{
            Globals.config = PropertyFileReader.getPropertyFileReader("src"+Globals.DEF_CONFIG_PATH);
        }
        logger.warn("Default Configurations Initialized");
    }
    
    public void initConfigurations(){
        String maxFiles = Globals.config.getProperty("tabs.max");
        String poolSize = Globals.config.getProperty("pool.undo.size");
        Globals.maxTabs = (validations.isInteger(maxFiles))?
                                    new Integer(maxFiles):Globals.maxTabs;
        Globals.poolSize = (validations.isInteger(poolSize))?
                             new Integer(poolSize):Globals.poolSize; 
    }

    public void loadLanguageBundle(){
        Globals.language = PropertyFileReader.getPropertyFileReader(getLanguageFileName());
        if(Globals.language == null){
            loadDefaultLanguage();
        }else{
            logger.info("Language Initialized");        
        }
    }
    
    private String getLanguageFileName(){
        return Globals.LANGUAGE_PATH + 
               Globals.config.getProperty("language.actual") +
               Globals.PROP_EXT;
    }
    
    private void loadDefaultLanguage(){
        if(Globals.IS_PROD){
            Globals.language = PropertyFileReader.getPropertyFileReader(
                    getClass().getResourceAsStream(Globals.DEF_LANGUAGE_PATH));
        }else{
            Globals.language = PropertyFileReader.getPropertyFileReader("src"+Globals.DEF_LANGUAGE_PATH);
        }
        logger.warn("Default Language Initialized");
    }
    
    public void loadImages(){
        Globals.logoImg = new ImageIcon(getClass().getResource(Globals.LOGO_PATH)).getImage();
        Globals.simfileIcon = new ImageIcon(getClass().getResource(Globals.SIM_ICON_PATH));
        Globals.textfileIcon = new ImageIcon(getClass().getResource(Globals.TEXT_ICON_PATH));
    }
    
    public void loadDecimalFormatter(){
        DecimalFormatSymbols symbols = new DecimalFormatSymbols();
        symbols.setDecimalSeparator('.');
        Globals.decimalFormat = new DecimalFormat("###.##",symbols);    
    }
    
    public void loadIde() {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                Globals.ideView = Ide.getInstance();
                Globals.ideView.setVisible(true);
            }
        });
    }

}
